package com.heima.homeservicemanagementsystem.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heima.homeservicemanagementsystem.config.AlipayConfig;
import com.heima.homeservicemanagementsystem.entity.Apientity;
import com.heima.homeservicemanagementsystem.entity.vo.AlipayResponse;
import com.heima.homeservicemanagementsystem.module.Result;
import com.heima.homeservicemanagementsystem.service.AliPayService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author jcf
 * @Date 2025/4/25
 */
@Slf4j
@Service
public class AliPayServiceImpl implements AliPayService {
    @Resource
    private AlipayConfig aliPayConfig;
    private static final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private static final String FORMAT = "JSON";
    private static final String CHARSET = "utf-8";
    private static final String SIGN_TYPE = "RSA2";

    @Override
    public Result refund(Apientity apientity) throws AlipayApiException, JsonProcessingException {
        log.info("支付宝退款接口调用开始：{}", apientity);
        final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        final String FORMAT = "JSON";
        final String CHARSET = "utf-8";
        final String SIGN_TYPE = "RSA2";
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(),
                aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE);
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(apientity.getTraceNo());//订单号
        model.setOutRequestNo(apientity.getTraceNo());
//        model.setTradeNo(" ");
        model.setRefundAmount(apientity.getTotalAmount());//订单金额
        request.setBizModel(model);
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
            ObjectMapper objectMapper = new ObjectMapper();
            AlipayResponse alipayResponse = objectMapper.readValue(response.getBody(), AlipayResponse.class);
            return Result.success(alipayResponse);
        }
        System.out.println("调用失败");

        // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
        // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
        // System.out.println(diagnosisUrl);

        return Result.error();
    }

    @Override
    public AlipayResponse getapliy(Apientity apientity) throws AlipayApiException, JsonProcessingException {
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(),
                aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(apientity.getTraceNo());
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");

        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        AlipayResponse alipayResponse = objectMapper.readValue(response.getBody(), AlipayResponse.class);
        return alipayResponse;
    }

    @Override
    public AlipayResponse refundBillDetails(Apientity apientity) throws JsonProcessingException, AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(),
                aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE);
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
        model.setOutTradeNo(apientity.getTraceNo());
        model.setOutRequestNo(apientity.getTraceNo());

        request.setBizModel(model);
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        AlipayResponse alipayResponse = objectMapper.readValue(response.getBody(), AlipayResponse.class);
        return alipayResponse;
    }
}
